package com.meixiang.adapter.personalCenter;

import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.activity.account.myShopper.OrderEvaluationActivity;
import com.meixiang.entity.EvaluateGradle;
import com.meixiang.entity.myOrder.EvaluateCommit;
import com.meixiang.entity.myOrder.MyOrderDetail;
import com.meixiang.global.GlobalConventionalData;
import com.meixiang.main.MXApplication;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.GlideHelper;
import com.meixiang.view.ClearEditText;
import com.meixiang.view.StarLayout;

import com.meixiang.view.photoSelectActivity.util.ImageItem;

import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author yinsujun  Created at 2016/5/10 11:37
 */
public class OrderEvaluationAdapter extends RecyclerView.Adapter<OrderEvaluationAdapter.ViewHolder>  {
    private OrderEvaluationActivity context ;
    private MyOrderDetail orderDetail=new MyOrderDetail();
    private List<EvaluateGradle> evaluateGradleList;
    public OrderEvaluationPhotoAdapter gridAdapter;

    private EvaluateCommit gevalParameter=new EvaluateCommit();
    private List<EvaluateCommit.GevalGoodsListBean> goodsList=new ArrayList<>();

    private int selectPosition=-1;

    public MyOrderDetail getOrderDetail() {
        return orderDetail;
    }

    public EvaluateCommit getGevalParameter() {
        return gevalParameter;
    }




    private  ArrayList<ImageItem> selectBitmap=new ArrayList<>();
    public void addSelectImage(ArrayList<ImageItem> selectBitmap){
        this.selectBitmap=selectBitmap;
        if(gridAdapter!=null){
            gridAdapter.notifyDataSetChanged();
        }
    }


    public OrderEvaluationAdapter(OrderEvaluationActivity context, MyOrderDetail orderDetail){
        this.context = context ;
        this.orderDetail=orderDetail;
        evaluateGradleList= MXApplication.mApp.getEvaluateGradle();
        if(evaluateGradleList==null){
            GlobalConventionalData.getEvaluateStart(context);
        }
    }
    public OrderEvaluationAdapter(OrderEvaluationActivity context){

        this.context = context ;
        evaluateGradleList= MXApplication.mApp.getEvaluateGradle();
        if(evaluateGradleList==null){
            GlobalConventionalData.getEvaluateStart(context);
        }
    }
    public void addPhotoItem(int photoPosition,List<String> filePath){
        if(orderDetail!=null){
            if(orderDetail.getOrderGoodsList().get(photoPosition).getPhoto()==null
                    ||orderDetail.getOrderGoodsList().get(photoPosition).getPhoto().size()==0){
                orderDetail.getOrderGoodsList().get(photoPosition).setPhoto(filePath);
            }else{
                orderDetail.getOrderGoodsList().get(photoPosition).getPhoto().addAll(filePath);
            }
            this.notifyDataSetChanged();
        }

    }
    public void addOrder(MyOrderDetail orderDetail){
        this.orderDetail=orderDetail;
        for(int i=0;i<orderDetail.getOrderGoodsList().size();i++){
            EvaluateCommit.GevalGoodsListBean evaluateData=new EvaluateCommit.GevalGoodsListBean();
            evaluateData.setGevalGoodsid(orderDetail.getOrderGoodsList().get(i).getGoodsId());
            goodsList.add(evaluateData);

        }

        this.notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.activity_order_evalution_list_item
                ,parent,false);
        ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }
    @SuppressWarnings("unchecked")
    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        this.selectPosition=position;
        gevalParameter.setGevalOrderid(orderDetail.getOrderId());
        final List<MyOrderDetail.OrderGoodsListBean> orderGoodsList = orderDetail.getOrderGoodsList();
        holder.tvName.setText(orderGoodsList.get(position).getGoodsName());
        MyOrderDetail.OrderGoodsListBean orderGoodsListBean = orderDetail.getOrderGoodsList().get(position);
        if(!AbStrUtil.isEmpty(orderGoodsListBean.getGoodsImage())){
            GlideHelper.showImage(context, orderGoodsListBean.getGoodsImage(),
                    R.drawable.image_default_gray_bg,holder.ivShopping);
        }
         List<MyOrderDetail.TagListBean> tagList = orderDetail.getOrderGoodsList().get(position).getTagId();
        if(tagList!=null&&tagList.size()>0){
            //设置标签相关数据
            MyTagAdapter tagAdapter=new MyTagAdapter(tagList,holder.flowLayout);
            holder.flowLayout.setAdapter(tagAdapter);
            TagViewClickListener listener=new TagViewClickListener(tagList);
            listener.setPosition(position);
            holder.flowLayout.setOnTagClickListener(listener);
        }

        if(orderGoodsList.get(position).getGevalList()!=null
                &&orderGoodsList.get(position).getGevalList().size()>0){
            holder.mStartDetails.setText(orderGoodsList.get(position).getGevalList().get(position).getScoresText());
        }
        holder.mStartLayout.setStarModel(StarLayout.MODEL_EDIT);
        holder.mStartLayout.starOnclickListener(new StarLayout.IStarOnclick() {
            @Override
            public void starNumber(int number) {
                goodsList.get(position).setScores(number+"");
                if(evaluateGradleList!=null){
                    for(int i=0;i<evaluateGradleList.size();i++){
                        if(number== Integer.parseInt(evaluateGradleList.get(i).getGrade())){
                            String scoresText=evaluateGradleList.get(i).getDescription();
                            holder.mStartDetails.setText(scoresText);
                            goodsList.get(position).setScoresText(scoresText);
                        }
                    }
                }
            }
        });

        gridAdapter=new OrderEvaluationPhotoAdapter(context,orderDetail.getOrderGoodsList().get(position).getGoodsName());
        GridLayoutManager gridLayoutManager=new GridLayoutManager(context,4);
        holder.gridRecycler.setLayoutManager(gridLayoutManager);
        holder.gridRecycler.setAdapter(gridAdapter);
        if(gridAdapter!=null){
            if(orderDetail.getOrderGoodsList().get(position).getPhoto()!=null
                    &&orderDetail.getOrderGoodsList().get(position).getPhoto().size()>0){
                this.gridAdapter.setPosition(position);
                this.gridAdapter.addPhoto((ArrayList<String>) orderDetail.getOrderGoodsList().get(position).getPhoto());
            }

        }

        //选择照片事件监听
        holder.mPhotoPrinting.setOnClickListener(new PhotoSelectListener(position));
        holder.etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                goodsList.get(position).setGevalContent(s.toString());

            }
        });
        gevalParameter.setGevalGoodsList(goodsList);

    }

    @Override
    public int getItemCount() {
        if(orderDetail.getOrderGoodsList()!=null&&orderDetail.getOrderGoodsList().size()>0){
            return orderDetail.getOrderGoodsList().size();
        }else
            return 0;
    }

    /**
     * 标签adapter
     */
    @SuppressWarnings("unchecked")
    private class MyTagAdapter extends TagAdapter{
        private TagFlowLayout flowLayout;
        private List<MyOrderDetail.TagListBean> tagList;
        public MyTagAdapter(List<MyOrderDetail.TagListBean> tagList,TagFlowLayout flowLayout) {
            super(tagList);
            this.tagList=tagList;
//            Log.e("初始化标签",selectPosition+"==标签选择=="+tagList.toString());
            this.flowLayout=flowLayout;
        }

        @Override
        public View getView(FlowLayout parent, int position, Object o) {

            final TextView textView = (TextView) LayoutInflater.from(context).inflate(R.layout.activity_order_evalution_tag, flowLayout, false);
            if (tagList != null && tagList.size() > 0) {
                if(tagList.get(position).isSelect()==true){
                    textView.setTextColor(ContextCompat.getColor(context, R.color.order_red_color));
                    textView.setBackgroundColor(ContextCompat.getColor(context, R.color.order_pink_color));
                }else{
                    textView.setTextColor(ContextCompat.getColor(context, R.color.black_8));
                    textView.setBackgroundColor(ContextCompat.getColor(context, R.color.order_evaluate_gray_color));
                }

            } else {
                textView.setTextColor(ContextCompat.getColor(context, R.color.black_8));
                textView.setBackgroundColor(ContextCompat.getColor(context, R.color.order_evaluate_gray_color));
            }
            textView.setText(tagList.get(position).getTagName());
            return textView;
        }
    }

    /**
     * 标签选择点击事件
     */
    private class TagViewClickListener implements  TagFlowLayout.OnTagClickListener{
        private List<MyOrderDetail.TagListBean> tagList;
        public TagViewClickListener(List<MyOrderDetail.TagListBean> tagList) {
            this.tagList = tagList;
        }
        private int selectPosition;
        public void setPosition(int selectPosition) {
            this.selectPosition = selectPosition;
        }
        @Override
        public boolean onTagClick(View view, int position, FlowLayout parent) {
            MyOrderDetail.TagListBean tagListBean = tagList.get(position);
                TextView tagView= (TextView) view;
                //如果已经选中了，选中标签有记录，设置为false,未选择的设置为true

                if(tagListBean.isSelect()==true){
                    orderDetail.getOrderGoodsList().get(selectPosition).getTagId().get(position).setSelect(false);
                    tagView.setTextColor(ContextCompat.getColor(context,R.color.black_8));
                    tagView.setBackgroundColor(ContextCompat.getColor(context,R.color.order_evaluate_gray_color));
                }else{
                    tagListBean.setSelect(true);
//                    tagId.add(tagList.get(position).getTagId());
                    tagView.setTextColor(ContextCompat.getColor(context,R.color.order_red_color));
                    tagView.setBackgroundColor(ContextCompat.getColor(context,R.color.order_pink_color));
                }
                Log.e("temp标签", selectPosition+"=======position==="+"\n============="
                +orderDetail.getOrderGoodsList().get(selectPosition).getTagId().toString());

//            for(int i=0;i<orderDetail.getOrderGoodsList().size();i++){
//                Log.e("评价===",i+"==位置=="+orderDetail.getOrderGoodsList().get(i).getTagId().toString());
//            }
                return false;
            }
    }


    private class PhotoSelectListener implements View.OnClickListener{
        private int position;

        public PhotoSelectListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            if(photoSelect!=null){
                photoSelect.onPhotoClickSelect(v,position);
            }
        }
    }
    private OnPhotoSelect photoSelect;
    public void setOnPhotoSelect(OnPhotoSelect photoSelect){
        this.photoSelect=photoSelect;
    }
    public interface OnPhotoSelect{
        void onPhotoClickSelect(View view,int position);
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @Bind(R.id.order_evaluation_iv_shopping)
        ImageView ivShopping ;
        @Bind(R.id.order_evaluation_tv_shopping_name)
        TextView tvName ;
        @Bind(R.id.order_evaluation_start)
        StarLayout mStartLayout ;
        @Bind(R.id.order_evaluation_tv_start_details)
        TextView mStartDetails ;
        @Bind(R.id.order_sale_linear_photo_printing)
        LinearLayout mPhotoPrinting ;
        @Bind(R.id.order_evaluation_flow_layout)
        TagFlowLayout flowLayout;
        @Bind(R.id.order_sale_et_problem_details)
        ClearEditText etContent;
        @Bind(R.id.grid_recycler)
        RecyclerView gridRecycler;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
